package com.carkeeper.controller;

import com.carkeeper.dto.UserDTO;
import com.carkeeper.exception.BussinessException;
import com.carkeeper.pojo.StoreUserPO;
import com.carkeeper.service.UserService;
import com.carkeeper.utils.JwtUtil;
import com.carkeeper.vo.LoginVO;
import com.carkeeper.vo.Pager;
import com.carkeeper.vo.ProfileVO;
import com.carkeeper.vo.UserVO;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author ：XXX
 * @date ：Created in 2020/5/26
 * @description ：
 * @version: 1.0
 */
@RestController
@RequestMapping("/storeUser")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param username
     * @param realname
     * @param phone
     * @param storeId
     * @return
     */
    @GetMapping
    public Pager<StoreUserPO> findAll(@RequestParam(value = "page", defaultValue = "1") Long page,
                                      @RequestParam(value = "pageSize", defaultValue = "10") Long pageSize,
                                      @RequestParam(value = "username", required = false) String username,
                                      @RequestParam(value = "realname", required = false) String realname,
                                      @RequestParam(value = "phone", required = false) String phone,
                                      @RequestParam(value = "storeId", required = false) Long storeId) {

        Pager<StoreUserPO> result = new Pager<>(userService.findAll(page, pageSize, username, realname, phone, storeId));
        return result;


    }


    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public UserVO queryUserById(@PathVariable Long id) {
        return userService.queryUserById(id);

    }

    /**
     * 增加用户
     *
     * @param userPojo
     * @return
     */
    @PostMapping("/addUser")
    public Boolean InsertUser(@RequestBody StoreUserPO userPojo) {
        userService.add(userPojo);
        return true;
    }

    /**
     * 删除用户
     *
     * @return boolean
     */
    @DeleteMapping("/{id}")
    public Boolean deleteUser(@PathVariable("id") Long id) {
        userService.delete(id);
        return true;
    }

    /**
     * 修改用户
     *
     * @param userPojo
     * @return
     */
    @PutMapping("/{id}")
    public Boolean updateUser(@PathVariable(value = "id") Long id, @RequestBody StoreUserPO userPojo) {
        userPojo.setId(id);
        userService.update(userPojo);
        return true;
    }


    @PostMapping("/list")
    public List<UserDTO> getByIds(@RequestBody List<Long> ids) {
        return userService.getByIds(ids);
    }


    /**
     * 登陆
     *
     * @param loginVO
     * @return
     */
    @PostMapping("/login")
    public LoginVO login(@RequestBody LoginVO loginVO) {
        if (StringUtils.isEmpty(loginVO.getUsername())) {
            throw new BussinessException("用户名不能为空");
        }
        if (StringUtils.isEmpty(loginVO.getPassword())) {
            throw new BussinessException("密码不能为空");

        }
        StoreUserPO storeUserPO = userService.login(loginVO.getUsername(), loginVO.getPassword());

        //登录成功生成jwt
        String jwt = JwtUtil.createJWT(storeUserPO.getUsername(), null);

        //将jwt返回
        LoginVO vo = new LoginVO();
        vo.setToken(jwt);
        return vo;
    }

    /**
     * 获取个人信息
     *
     * @return
     */
    @GetMapping("/profile")
    public ProfileVO profile(@RequestHeader("Authorization") String authorization) throws Exception {
        String token = authorization.replace("VEA-ADMIN ", "");
        Claims claims = JwtUtil.parseJWT(token);
        String subject = claims.getSubject();
        ObjectMapper om = new ObjectMapper();
        Map<String, Object> claimsMap = om.readValue(subject, Map.class);
        String username = String.valueOf(claimsMap.get("username"));
        StoreUserPO po = userService.findByUsername(username);
        ProfileVO vo = new ProfileVO();
        vo.setUsername(po.getUsername());
        vo.setId(po.getId());
        vo.setName(po.getRealname());
        vo.setStoreId(po.getStoreId() + "");
        return vo;
    }
}
